package com.logic.saas.util;

import java.util.Collection;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;


public class JsonUtils {

	private JsonUtils() {
		super();
	}

	
	public static String toJSON(final Object target) throws Exception {
		return toJSON(target, null, null);
	}

	
	public static String toJSON(final Object target, final String datePattern) throws Exception {
		return toJSON(target, null, datePattern);
	}

	
	public static String toJSON(final Object target, final String[] excludes) throws Exception {
		return toJSON(target, excludes, null);
	}

	
	public static String toJSON(final Object target, final String[] excludes,
			final String datePattern)throws Exception {
		JsonConfig config = new JsonConfig();
		if (null != excludes && 0 != excludes.length) {
			config.setExcludes(excludes);
		}

		if (null != datePattern && !datePattern.trim().equals("")) {
			
			config.registerJsonValueProcessor(java.util.Date.class,
					new JsonValueProcessor() {
						public Object processArrayValue(Object obj,
								JsonConfig jsonconfig) {
							return this.process(obj);
						}

						public Object processObjectValue(String s, Object obj,
								JsonConfig jsonconfig) {
							return this.process(obj);
						}

						private String process(Object obj) {
							if (null == obj) {
								return "";
							}
							java.util.Date date = (java.util.Date) obj;
							java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
									datePattern.trim());
							return format.format(date);
						}
					});
			
			config.registerJsonValueProcessor(java.sql.Timestamp.class,
					new JsonValueProcessor() {
						public Object processArrayValue(Object obj,
								JsonConfig jsonconfig) {
							return this.process(obj);
						}

						public Object processObjectValue(String s, Object obj,
								JsonConfig jsonconfig) {
							return this.process(obj);
						}

						private String process(Object obj) {
							if (null == obj) {
								return "";
							}
							java.sql.Timestamp date = (java.sql.Timestamp) obj;
							java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
									datePattern.trim());
							return format.format(date);
						}
					});
		}

		String json = null;
		if (target instanceof Collection || target instanceof Object[]) {
			JSONArray jsonArray = JSONArray.fromObject(target, config);
			json = jsonArray.toString();
		} else {
			JSONObject jsonObject = JSONObject.fromObject(target, config);
			json = jsonObject.toString();
		}
		return json;
	}
	
	
	/**
	 * json数组转成java数组
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static Object[] getDTOArray(String jsonString, Class clazz){     
        JSONArray array = JSONArray.fromObject(jsonString);     
        Object[] obj = new Object[array.size()];     
        for(int i = 0; i < array.size(); i++){     
            JSONObject jsonObject = array.getJSONObject(i);     
            obj[i] = JSONObject.toBean(jsonObject, clazz);     
        }     
        return obj;     
    }    
}
